/*******************************************************************************

This code file produces Figure A29, "Correlates of Neighborhood MVPF Percentile Rank."

*******************************************************************************/

*** Manage settings

	run "$dir/code/modules/settings.do"

********************************************************************************
* Main
********************************************************************************	
	
*** Construct dataset

	local vlist "medage sh_renter sh_hispanic sh_white sh_black sh_asian sh_vacant lpopdens sh_lths sh_hsdeg sh_somecoll sh_coll sh_postgrad sh_drive sh_bus sh_subway sh_walk sh_poor lmedhhinc lmeangrossrent"

	* Collapse block characteristics to neighborhood means
	use "$data/clean/cleaned_data.dta", clear

	collapse (mean) `vlist', by(nta_code nta)

	* Merge in MVPFs by neighborhood
	merge 1:1 nta_code using  "$data/clean/mvpf_by_nta.dta", nogen keep(3)
	
	* Create rank variables
	
	foreach v of varlist mvpf `vlist' {
		
		egen rk_`v' = rank(`v')
		summ rk_`v'
		replace rk_`v' = (rk_`v'-r(min))/(r(max)-r(min))
		
	}
	
*** Produce binscatters

	binscatter rk_mvpf rk_medage, line(none) ylab(,nogrid) name(medage, replace) graphregion(lcolor(white)) xtitle("Median Age") ytitle("")
	binscatter rk_mvpf rk_sh_renter, line(none) ylab(,nogrid) name(sh_renter, replace) graphregion(lcolor(white)) xtitle("Share Renter") ytitle("")
	binscatter rk_mvpf rk_sh_hispanic, line(none) ylab(,nogrid) name(sh_hispanic, replace) graphregion(lcolor(white)) xtitle("Share Hispanic") ytitle("")
	binscatter rk_mvpf rk_sh_white, line(none) ylab(,nogrid) name(sh_white, replace) graphregion(lcolor(white)) xtitle("Share White") ytitle("")
	binscatter rk_mvpf rk_sh_black, line(none) ylab(,nogrid) name(sh_black, replace) graphregion(lcolor(white)) xtitle("Share Black") ytitle("")
	binscatter rk_mvpf rk_sh_asian, line(none) ylab(,nogrid) name(sh_asian, replace) graphregion(lcolor(white)) xtitle("Share Asian") ytitle("")
	binscatter rk_mvpf rk_sh_vacant, line(none) ylab(,nogrid) name(sh_vacant, replace) graphregion(lcolor(white)) xtitle("Share Vacant") ytitle("")
	binscatter rk_mvpf rk_lpopdens, line(none) ylab(,nogrid) name(lpopdens, replace) graphregion(lcolor(white)) xtitle("Population Density") ytitle("")
	binscatter rk_mvpf rk_sh_lths, line(none) ylab(,nogrid) name(sh_lths, replace) graphregion(lcolor(white)) xtitle("Share Less than High School") ytitle("")
	binscatter rk_mvpf rk_sh_hsdeg, line(none) ylab(,nogrid) name(sh_hsdeg, replace) graphregion(lcolor(white)) xtitle("Share High School Grad") ytitle("")
	binscatter rk_mvpf rk_sh_somecoll, line(none) ylab(,nogrid) name(sh_somecoll, replace) graphregion(lcolor(white)) xtitle("Share Some College") ytitle("")
	binscatter rk_mvpf rk_sh_coll, line(none) ylab(,nogrid) name(sh_coll, replace) graphregion(lcolor(white)) xtitle("Share BA") ytitle("")
	binscatter rk_mvpf rk_sh_postgrad, line(none) ylab(,nogrid) name(sh_postgrad, replace) graphregion(lcolor(white)) xtitle("Share More than BA") ytitle("")
	binscatter rk_mvpf rk_sh_drive, line(none) ylab(,nogrid) name(sh_drive, replace) graphregion(lcolor(white)) xtitle("Share Drive") ytitle("")
	binscatter rk_mvpf rk_sh_bus, line(none) ylab(,nogrid) name(sh_bus, replace) graphregion(lcolor(white)) xtitle("Share Bus") ytitle("")
	binscatter rk_mvpf rk_sh_subway, line(none) ylab(,nogrid) name(sh_subway, replace) graphregion(lcolor(white)) xtitle("Share Subway") ytitle("")
	binscatter rk_mvpf rk_sh_walk, line(none) ylab(,nogrid) name(sh_walk, replace) graphregion(lcolor(white)) xtitle("Share Walk") ytitle("")
	binscatter rk_mvpf rk_sh_poor, line(none) ylab(,nogrid) name(sh_poor, replace) graphregion(lcolor(white)) xtitle("Share Poor") ytitle("")
	binscatter rk_mvpf rk_lmedhhinc, line(none) ylab(,nogrid) name(lmedhhinc, replace) graphregion(lcolor(white)) xtitle("Median HH Income") ytitle("")
	binscatter rk_mvpf rk_lmeangrossrent, line(none) ylab(,nogrid) name(lmeangrossrent, replace) graphregion(lcolor(white)) xtitle("Mean Gross Rent") ytitle("")
	
	graph combine `vlist', ycommon xcommon graphregion(color(white)) cols(4) ysize(6)
	graph export "$figs/mvpf_correlates.pdf", replace
